package net.lzzy.practices.web.courses;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Massages;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.CourseService;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.StringUtil;
import net.lzzy.practices.web.models.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/*
 * @author admin
 */
@Api("课程相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_COURSE_ROOT)
public class CourseApiController {
    private  final CourseService service;
    private  final UserService uService;
    public CourseApiController(CourseService services, UserService uService) {
        this.service = services;
        this.uService = uService;
    }
    @ApiOperation("查询课表")
    @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,
            paramType = "query", required = true)
    @GetMapping(ApiConstant.ROUTE_COURSE_ALL)
    public ApiResult getCourses(@RequestParam String token){
        List<VmCourse> courses =new ArrayList<>();
        service.get().forEach(course->courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size()+"",courses);
    }
    @ApiOperation("查询公开课")
    @GetMapping(ApiConstant.ROUTE_COURSE_OPEN)
    public ApiResult getOpenCourses(@RequestParam String token){
        List<VmCourse> courses =new ArrayList<>();
        service.get().forEach(course->courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size()+"",courses);
    }
    @ApiOperation("分页查询公开课")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",dataType = "int",dataTypeClass = Integer.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "size",value = "每页数量",dataType = "int",dataTypeClass = Integer.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,
                    paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_COURSE_PAGE)
    public ApiResult getPagedCourses(@RequestParam int page,@RequestParam int size,@RequestParam String token){
        List<VmCourse> courses =new ArrayList<>();
        service.get(page,size).forEach(course->courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size()+"",courses);
    }
    @ApiOperation("分页查询公开课")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",dataType = "int",dataTypeClass = Integer.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "size",value = "每页数量",dataType = "int",dataTypeClass = Integer.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_COURSE_OPEN_PAGE)
    public ApiResult getPagedOpenCourses(@RequestParam int page,@RequestParam int size,@RequestParam String token){
        List<VmCourse> courses =new ArrayList<>();
        service.get(page,size,true).forEach(course->courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size()+"",courses);
    }
    @ApiOperation("查询教师课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId",value = "教师id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_COURSE_TEACHER)
    public ApiResult getTeacherCourses(@RequestParam String token,@RequestParam UUID teacherId){
        List<VmCourse> courses =new ArrayList<>();
        service.getTeacherCourses(teacherId).forEach(course->courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size()+"",courses);
    }
    @ApiOperation("查询学生课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId",value = "学生id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_COURSE_STUDENT)
    public ApiResult getStudentCourses(@RequestParam String token,@RequestParam UUID studentId){
        List<VmCourse> courses =new ArrayList<>();
        service.getStudentsCourse(studentId).forEach(course->courses.add(VmCourse.create(course,false)));
        return new ApiResult(true,courses.size()+"",courses);
    }
    private ApiResult addOrUpdate(UUID id, CourseInfo info){
        String token=info.getToken();
        String name=info.getName();
        String desc=info.getDesc();
        String cover=info.getCover();
        String teacherId=info.getTeacherId();
        boolean open=info.isOpen();
//进行完整性检查
        if (StringUtil.isEmpty(name)||
                StringUtil.isEmpty(desc)||
                StringUtil.isEmpty(cover)||
                StringUtil.isEmpty(teacherId)){
            return new ApiResult(false, Massages.MISS_ARGS.toString(), null);
        }
        User teacher=uService.getById(UUID.fromString(teacherId));
        if (teacher==null){
            return new ApiResult(false, Massages.WRONG_ID.toString(), null);
        }

        Course course;
        boolean add = id ==null;
        if (add){
            course=new Course();
            course.setTeacher(teacher);
        }else {
            course=service.getById(id);
            if (course==null){
                return new ApiResult(false,Massages.WRONG_ID.toString(), null);
            }
        }
        //调用业务方法
        course.setUpdateTime(new Date());
        course.setOpen(open);
        course.setName(name);
        course.setDescription(desc);
        if (add){
            return new ApiResult(true,Massages.SUCCESS.toString(), VmCourse.create(service.add(course),true));
        }else {
            Course updated=service.update(course,token);
            if (updated==null){
                return new ApiResult(false, Massages.WRONG_ID.toString(), null);
            }else {
                return new ApiResult(true,Massages.SUCCESS.toString(), VmCourse.create(updated,true));
            }
        }
    }
    //增加课程
    @ApiOperation("增加课程")
    @ApiImplicitParam(name = "info",value = "课程信息",dataType = "CourseInfo",dataTypeClass = CourseInfo.class,paramType = "body", required = true)
    @PostMapping(ApiConstant.ROUTE_COURSE_ADD)
    public ApiResult add(@RequestBody CourseInfo info){
        return addOrUpdate(null,info);
    }
    //修改课程
    @ApiOperation("修改课程")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "课程id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "info",value = "课程信息",dataType = "CourseInfo",dataTypeClass = CourseInfo.class,paramType = "body", required = true)})
    @PutMapping(ApiConstant.ROUTE_COURSE_PUT)
    public ApiResult update(@PathVariable UUID id,@RequestBody CourseInfo info){
        return addOrUpdate(id,info);
    }
    ////开关课程
    @ApiOperation("开关课程")
    @ApiImplicitParam(name = "info",value = "课程信息",dataType = "CourseOpenInfo",dataTypeClass = CourseOpenInfo.class,paramType = "body", required = true)
    @PostMapping(ApiConstant.ROUTE_COURSE_OPEN_CLOSE)
    public  ApiResult openClose(@RequestBody CourseOpenInfo info){
        String token = info.getToken();
        String strCourseId = info.getCid();
        boolean open = info.isOpen();
        if (StringUtil.isEmpty(token)||StringUtil.isEmpty(strCourseId)){
            return new ApiResult(false,Massages.MISS_ARGS.toString(),null);
        }
        Course course=service.openCloseCourse(UUID.fromString(strCourseId),open,token);
        if (course==null){
            return new ApiResult(false,Massages.NO_PERMISSION.toString(),null);
        }
        return new ApiResult(true,Massages.SUCCESS.toString(),VmCourse.create(course,true));
    }
    //删除课程
    @ApiOperation("删除课程")
    @ApiImplicitParam(name = "info",value = "课程信息",dataType = "CourseKeyInfo",dataTypeClass = CourseKeyInfo.class,paramType = "body", required = true)
    @DeleteMapping(ApiConstant.ROUTE_COURSE_REMOVE)
    public ApiResult delete(@RequestBody CourseKeyInfo info){
        String token=info.getToken();
        String strCourseId=info.getCid();
        //参数检查
        if (StringUtil.isEmpty(token)||StringUtil.isEmpty(strCourseId)){
            return new ApiResult(false,Massages.MISS_ARGS.toString(),null);
        }
        try {
            UUID courseId=UUID.fromString(strCourseId);
            Massages msg=service.remove(courseId,token);
            return new ApiResult(msg==Massages.SUCCESS,msg.toString(),null);
        }catch (Exception e){
            return new ApiResult(false,Massages.WRONG_ID.toString(),null);
        }

    }
}
